package com.viewpagertext.utils;

import com.viewpagertext.DBHelper.AppDatabase;
import com.viewpagertext.DBHelper.Dao.DownloadMusicDao;
import com.viewpagertext.Model.DownloadMusic;
import com.viewpagertext.constructor.Song;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class LoadLocalMusicUtils {
    public static List<Song> getMusic(){
        DownloadMusicDao dao = AppDatabase.getInstance().downloadMusicDao();
        List<DownloadMusic> localSongList = dao.getAllDownloadMusic();
        List<Song> songList = new ArrayList<>();
        for(DownloadMusic localSong : localSongList){
            Song song = new Song();
            song.song = localSong.name;
            song.singer = localSong.singer;
            song.size = getSize(localSong.path);
            song.path = localSong.path;
            song.album = localSong.album;
            song.duration = getDuration(localSong.time);
            song.lrc = localSong.lrc;
            song.picPath = localSong.pic;
            songList.add(song);
        }
        return songList;
    }

    // 获取文件大小
    public static long getSize(String filePath){
        File file = new File(filePath);
        if (file.exists()) return file.length();
        else return 0;
    }

    // 转换时间
    public static int getDuration(String time){
        String[] parts = time.split(":"); // 将字符串分割成分钟和秒
        if (parts.length != 2) {
            throw new IllegalArgumentException("时间格式不正确");
        }
        int minutes = Integer.parseInt(parts[0]); // 获取分钟
        int seconds = Integer.parseInt(parts[1]); // 获取秒

        return minutes * 60 + seconds; // 将分钟转换为秒并加上剩余的秒数
    }
}
